﻿using System.IO;
using System.Windows;
using System.Windows.Controls;
using ParseControls;

namespace ParseOnlineAdmin
{
    /// <summary>
    /// Interaction logic for TheAccountView.xaml
    /// </summary>
    public partial class TheAccountView : UserControl
    {
        public TheAccountView()
        {
            InitializeComponent();
        }

        // Events 
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            ParseOne.Package.UserName = txtUserName.Text;
            ParseOne.Package.EMail = txtMail.Text;
            ParseOne.Package.ImageUrl = txtImageUrl.Text;
            ParseOne.Package.BackgroundImageUrl = this.txtBackgroundImageUrl.Text;

            ParseOne.Package.Save();

            ParseOne.InformationSending(new ParseCustomEventArgs("Account:Refresh"));

            // Close Panel 
            ParseOne.InformationSending(new ParseCustomEventArgs("Panel:Close"));
        }
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            // Close Panel 
            ParseOne.InformationSending(new ParseCustomEventArgs("Panel:Close"));
        }

        // overrides 
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            txtUserName.Text = ParseOne.Package.UserName;
            txtMail.Text = ParseOne.Package.EMail;
            txtImageUrl.Text = ParseOne.Package.ImageUrl;
            txtBackgroundImageUrl.Text = ParseOne.Package.BackgroundImageUrl;

            this.PreviewDragEnter += FileBorder_PreviewDragEnter;
            this.PreviewDrop += FileBorder_PreviewDrop;
        }

        private void FileBorder_PreviewDragEnter(object sender, DragEventArgs e)
        {
            bool isCorrect = true;

            if (e.Data.GetDataPresent(DataFormats.FileDrop, true) == true)
            {
                string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop, true);
                foreach (string filename in filenames)
                {
                    if (File.Exists(filename) == false)
                    {
                        isCorrect = false;
                        break;
                    }
                    //FileInfo info = new FileInfo(filename);
                    //if (info.Extension != ".txt")
                    //{
                    //    isCorrect = false;
                    //    break;
                    //}
                }
            }
            if (isCorrect == true)
                e.Effects = DragDropEffects.All;
            else
                e.Effects = DragDropEffects.None;
            e.Handled = true;
        }
        private void FileBorder_PreviewDrop(object sender, DragEventArgs e)
        {
            string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop, true);
            if (filenames.Length >= 1)
            {
                foreach (string file in filenames)
                {
                    if (!File.Exists(file))
                        continue;

                    this.txtBackgroundImageUrl.Text = file;
                }
            }

            // ParseOne.InformationSending(new ParseCustomEventArgs("RelatedFiles:Reload"));

            e.Handled = true;
        }
    }
}
